<?php
# Author    :  ELIZAR M. FLORES
# Copyright : Copyright? what do you mean copyright? The best thing in programming is OPEN SOURCE. So go ahead dig in.
#             If you know what you are doing, MODIFY and IMPROVE.
#             If you dont know what you are doing, READ and ASK.
#             If you are a hacker, Go somewhere else. 
#             If you are rich, Go ahead... DONATE!

class SESSION_HANDLER extends QUERY
{
    public function __construct() {
        session_start();
    }
    
    private function CHECK_ACCOUNT($email,$password)
    {
      $key     = array('email'=>$email,'password'=>$password);
      $cols    = array('member_id');
      $account = new QUERY(array('TABLE'=>'member','KEY'=>$key,'COLS'=>$cols));
      
      $result = $account->numRows() > 0 ? $account->fetch('member_id'):false;
      return $result;
    }    
    
    /*--PUBLIC FUNCTIONS GOES HERE---------------------------------------------------------------------------------------------------*/
    
    public function set_session($email,$password,$url='user/dashboard',$restricted='user/restricted')
     {
        $result = $this->CHECK_ACCOUNT($email, $password);
        if($result) 
          {
            $_SESSION['id']     = $result;
            $_SESSION['logged'] = true;
            
            redirect($url);
          }
        else
          {
            redirect($restricted);    
          }
     }
     
     public function create_sessions($vars = array())
      {
           foreach($vars as $key => $value)
            {
              $_SESSION[$key] = $value;    
            }
      }
     
     public function check_session($vars = array())
      {
         $result = true;
         foreach($vars as $var)
             {
                if(!isset($_SESSION[$var]))
                    {
                      $result = false;
                      break;
                    }  
             }
         return $result;    
      }
}

?>